
#get list of variable cost
get_l_variable_cost <- function(l_cost,l_v_cost){
  for (i in c(1:length(l_cost))) {
    if (grepl("recovered",names(l_cost[i])) == TRUE) l_cost[i] <- 0
    if (grepl("\\.trt",names(l_cost[i])) == TRUE) l_cost[i] <- l_v_cost$out_trt_EP
    if ((grepl("\\.trt",names(l_cost[i])) & grepl("EP",names(l_cost[i]))) == TRUE) l_cost[i] <- l_v_cost$out_trt_EP
    if ((grepl("\\.trt",names(l_cost[i])) & grepl("EN",names(l_cost[i]))) == TRUE) l_cost[i] <- l_v_cost$out_trt_EN
    if ((grepl("\\.trtf",names(l_cost[i])) & grepl("EP",names(l_cost[i]))) == TRUE) l_cost[i] <- l_v_cost$out_trt_EP
    if ((grepl("\\.trtf",names(l_cost[i])) & grepl("EN",names(l_cost[i]))) == TRUE) l_cost[i] <- l_v_cost$out_trt_EN
    if ((grepl("\\.ntrt",names(l_cost[i])) & grepl("EP",names(l_cost[i]))) == TRUE) l_cost[i] <- l_v_cost$out_ntrt_EP
    if ((grepl("\\.ntrt",names(l_cost[i])) & grepl("EN",names(l_cost[i]))) == TRUE) l_cost[i] <- l_v_cost$out_ntrt_EN
    if ((grepl("\\.ntrt",names(l_cost[i])) & grepl("GZ",names(l_cost[i]))) == TRUE) l_cost[i] <- l_v_cost$out_ntrt_EN
    if ((grepl("\\.trt",names(l_cost[i])) & grepl("GZ",names(l_cost[i]))) == TRUE) l_cost[i] <- l_v_cost$out_trt_EN
    if ((grepl("\\.trtf",names(l_cost[i])) & grepl("GZ",names(l_cost[i]))) == TRUE) l_cost[i] <- l_v_cost$out_trt_EN
    if (grepl("CC.trt",names(l_cost[i])) == TRUE & grepl("DCC",names(l_cost[i])) == FALSE & grepl("HCC",names(l_cost[i])) == FALSE) l_cost[i] <- l_v_cost$out_trt_EN + l_v_cost$Cirr
    if (grepl("CC.ntrt",names(l_cost[i])) == TRUE & grepl("DCC",names(l_cost[i])) == FALSE & grepl("HCC",names(l_cost[i])) == FALSE) l_cost[i] <- l_v_cost$out_ntrt_EN
    if (grepl("DCC",names(l_cost[i])) == TRUE) l_cost[i] <- l_v_cost$DCC
    if (grepl("HCC",names(l_cost[i])) == TRUE) l_cost[i] <- l_v_cost$HCC
  }
  return(l_cost)
}

#get variable cost
get_var_cost <- function(markov,l_cost){
  z <- 0
  sum_markov <- lapply(markov[,c(1:ncol(markov)-1)],sum)
  for (i in c(1:length(l_cost))){
    z <- z + sum_markov[[i]]*l_cost[[i]]
  }
  return(z)
}
# get variable cost at year X
get_var_cost_year <- function(markov,l_cost,year){
  markov_th <- markov[c(1:year),]
  z <- 0
  sum_markov <- lapply(markov_th[,c(1:ncol(markov_th)-1)],sum)
  for (i in c(1:length(l_cost))){
    z <- z + sum_markov[[i]]*l_cost[[i]]
  }
  return(z)
}

#Get var cost discount
get_var_cost_disc <- function(markov, l_cost, disc=0.03){
  discount_YLD <- exp(-disc*c(0:(nrow(markov)-1)))
  cost_by_HS <- c(unlist(l_cost),0)
  cost_matrix <- matrix(rep(cost_by_HS,nrow(markov)),nrow = nrow(markov),ncol = ncol(markov),byrow = T)
  cost_matrix_disc <- cost_matrix*discount_YLD
  cost_matrix_disc_markov <- cost_matrix_disc*markov
  sum_all_disc <- lapply(cost_matrix_disc_markov,sum)
  var_cost_disc <- sum(unlist(sum_all_disc))
  return(var_cost_disc)
}


#Get fixed cost

get_fixed_cost <- function(cost,cycle){
  fixed_cost = cost*cycle
  return(fixed_cost)
}

#Get fixed cost discounted

get_fixed_cost_disc <- function(cost, n_cycle,disc){
  discount_cost <- exp(-disc*c(0:(n_cycle-1)))
  fixed_cost <- rep(cost,n_cycle)
  fixed_cost_disc <- fixed_cost*discount_cost
  fixed_cost_disc_f <- sum(fixed_cost_disc)
  return(fixed_cost_disc_f)
}

# Get cost detail
get_cost_detail <- function(l_v_cost_detail,trace){
  l_cost_detail <- vector("list",length = length(l_v_cost_detail))
  names(l_cost_detail) <- names(l_v_cost_detail)
  for (i in c(1:length(trace))){
    if (grepl("\\.trt",names(trace[i])) == TRUE | grepl("\\.trtf",names(trace[i])) == TRUE) l_cost_detail$TDF[i] <- l_v_cost_detail$TDF*trace[i] else l_cost_detail$TDF[i] <- 0
    if (grepl("EN",names(trace[i])) == TRUE & grepl("\\.ntrt",names(trace[i])) == TRUE) l_cost_detail$FU_EN_ntrt[i] <- l_v_cost_detail$FU_EN_ntrt*trace[i] else l_cost_detail$FU_EN_ntrt[i] <- 0
    if (grepl("EN",names(trace[i])) == TRUE & grepl("\\.ntrt",names(trace[i])) == FALSE) l_cost_detail$FU_EN_trt[i] <- l_v_cost_detail$FU_EN_trt*trace[i] else l_cost_detail$FU_EN_trt[i] <- 0
    if (grepl("EP",names(trace[i])) == TRUE & grepl("\\.ntrt",names(trace[i])) == TRUE) l_cost_detail$FU_EP_ntrt[i] <- l_v_cost_detail$FU_EP_ntrt*trace[i] else l_cost_detail$FU_EP_ntrt[i] <- 0
    if (grepl("EP",names(trace[i])) == TRUE & grepl("\\.ntrt",names(trace[i])) == FALSE) l_cost_detail$FU_EP_trt[i] <- l_v_cost_detail$FU_EP_trt*trace[i] else l_cost_detail$FU_EP_trt[i] <- 0
    if (grepl("CC",names(trace[i])) == TRUE & grepl("DCC",names(trace[i])) == FALSE & grepl("HCC",names(trace[i])) == FALSE) l_cost_detail$hospit_Cirr[i] <- l_v_cost_detail$hospit_Cirr*trace[i] else l_cost_detail$hospit_Cirr[i] <- 0
    if (grepl("DCC",names(trace[i])) == TRUE) l_cost_detail$hospit_DCC[i] <- l_v_cost_detail$hospit_DCC*trace[i] else l_cost_detail$hospit_DCC[i] <- 0
    if (grepl("HCC",names(trace[i])) == TRUE) l_cost_detail$HCC[i] <- l_v_cost_detail$HCC*trace[i] else l_cost_detail$HCC[i] <- 0
  }
  df_cost_detail <- as.data.frame(l_cost_detail)
  cost_detail <- apply(df_cost_detail,2,sum)
  return(cost_detail)
}
